package org.xiaov.consumer.controller;

import org.springframework.web.bind.annotation.*;
import org.xiaov.consumer.client.ProviderClient;
import org.xiaov.entity.po.UserPo;
import org.xiaov.framework.constant.ApiR;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 描述：
 * </p>
 *
 * @author xiaovcloud
 * @since 2021/12/19 15:17
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private ProviderClient providerClient;

    /**
     * 获取所有用户
     *
     * @return 用户集合
     */
    @GetMapping
    public ApiR<List<UserPo>> getAllUser() {
        ApiR<List<UserPo>> allUser = providerClient.getAllUser();
        return ApiR.okMsgAndData(allUser.getMsg(), allUser.getData());
    }

    /**
     * 根据 id 获取用户
     *
     * @param id id
     * @return 用户
     */
    @GetMapping("/{id}")
    public ApiR<UserPo> getUserById(@PathVariable String id) {
        ApiR<UserPo> user = providerClient.getUserById(id);
        return ApiR.okMsgAndData(user.getMsg(), user.getData());
    }

    /**
     * 保存用户
     *
     * @param userPo 用户信息
     * @return 返回ApiR<Boolean>
     */
    @PostMapping()
    public ApiR<Boolean> saveUser(@RequestBody UserPo userPo) {
        ApiR<Boolean> flag = providerClient.saveUser(userPo);
        return ApiR.msg(flag.isSuccess(), flag.getMsg(), flag.getMsg());
    }

    /**
     * 根据 id 修改用户
     *
     * @param userPo 用户信息
     * @return ApiR<Boolean>
     */
    @PutMapping()
    public ApiR<Boolean> edit(@RequestBody UserPo userPo) {
        ApiR<Boolean> edit = providerClient.edit(userPo);
        return ApiR.msg(edit.isSuccess(), edit.getMsg(), edit.getMsg());
    }

    /**
     * 根据id删除用户
     *
     * @param id id
     * @return ApiR<Boolean>
     */
    @DeleteMapping("/{id}")
    public ApiR<Boolean> delete(@PathVariable String id) {
        ApiR<Boolean> delete = providerClient.delete(id);
        return ApiR.msg(delete.isSuccess(), delete.getMsg(), delete.getMsg());
    }


}
